﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business.Models;
using System.Data;
using System.Data.Entity.Infrastructure;

namespace Business.DAO
{
    public class TruongHocDAO
    {
        private ISSContext db = new ISSContext();
        public List<TruongHoc> GetAll()
        {
            return db.TruongHocs.ToList();
        }

        public TruongHoc Get(int id)
        {
            TruongHoc TruongHoc = db.TruongHocs.Find(id);
            return TruongHoc;
        }

        public int Create(TruongHoc TruongHoc)
        {
            try
            {
                db.TruongHocs.Add(TruongHoc);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(TruongHoc truongHoc)
        {
            try
            {
                truongHoc.NgayTao = DateTime.Now;
                db.Entry(truongHoc).State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                TruongHoc TruongHoc = db.TruongHocs.Find(id);
                db.TruongHocs.Remove(TruongHoc);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public List<TruongHoc> GetByDiaDiem(DiaDiem DiaDiem)
        {
            var query = from truonghoc in db.TruongHocs where truonghoc.diaDiem == DiaDiem select truonghoc;
            List<TruongHoc> lstTruongHoc = query.ToList<TruongHoc>();
            return lstTruongHoc;
        }

        public List<TruongHoc> GetByDaiLy(DaiLy DaiLy)
        {
            var query = from truonghoc in db.TruongHocs where truonghoc.daiLy == DaiLy select truonghoc;
            List<TruongHoc> lstTruongHoc = query.ToList<TruongHoc>();
            return lstTruongHoc;
        }

        public List<TruongHoc> GetByDiaDiem_DaiLy(DiaDiem DiaDiem, DaiLy DaiLy)
        {
            var query = from truonghoc in db.TruongHocs where truonghoc.daiLy == DaiLy && truonghoc.diaDiem == DiaDiem select truonghoc;
            List<TruongHoc> lstTruongHoc = query.ToList<TruongHoc>();
            return lstTruongHoc;
        }
    }
}